package com.hhh.demo.sx.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hhh.demo.sx.bean.FrontData;
import com.hhh.demo.sx.bean.LayuiTree;

import com.hhh.demo.sx.bean.RoleMenu;
import com.hhh.demo.sx.service.RoleMenuService;
import com.hhh.demo.sx.util.ResultMap;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.websocket.server.PathParam;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hhh
 * @since 2021-12-27
 */
@RestController
@RequestMapping("/role_menu")
public class RoleMenuController {

    @Autowired
    private RoleMenuService roleMenuService;


    /**
     *
     * 逻辑待完善
     * @param mid
     * @param children
     * @param f
     * @param rid
     * @return
     */
    @Transactional
    @RequestMapping("/add")
    public ResultMap add(int mid, String children, boolean f, int rid) {
        QueryWrapper<RoleMenu> wrapper = new QueryWrapper<>();
        String[] ids=children.split(",");

         if (f) {
             wrapper.eq("role_id", rid)
                     .eq("menu_id", mid);
             if (roleMenuService.getOne(wrapper)==null){
                 roleMenuService.saveOrUpdate(new RoleMenu(rid,mid));
                 wrapper.clear();
             }

            if (!children.equals("")) {
                for (String l : ids) {
                    wrapper.eq("role_id", rid)
                            .eq("menu_id", Integer.parseUnsignedInt(l));
                    if (roleMenuService.getOne(wrapper)==null){
                        roleMenuService.saveOrUpdate(new RoleMenu(rid, Integer.parseUnsignedInt(l)));
                        wrapper.clear();
                    }
                }
            }

        } else {

            wrapper.eq("role_id", rid)
                    .eq("menu_id", mid);
            roleMenuService.remove(wrapper);
            wrapper.clear();
            if (!children.equals("")) {
                for (String l : ids) {
                    wrapper.eq("role_id", rid)
                            .eq("menu_id", l);
                    roleMenuService.remove(wrapper);
                    wrapper.clear();
                }
            }
        }
        return ResultMap.success();
    }
}

